package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    //添加新的套餐或菜品,以及增加数量
    @PostMapping("/add")
    public R<ShoppingCart> save(@RequestBody ShoppingCart shoppingCart){
        //查询是否购物车中已经有了这个套餐或菜品
        //先判断是菜品还是套餐

        //获取当前登录的用户的id
        Long userId = BaseContext.getCurrentId();
        //设置当前订单的用户id
        shoppingCart.setUserId(userId);
        //获取菜品id
        Long dishId = shoppingCart.getDishId();
        //获取套餐id
        Long setmealId = shoppingCart.getSetmealId();
        //创建查询条件
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        //只查当前用户的
        queryWrapper.eq(ShoppingCart::getUserId,userId);

        //如果是菜品，就根据菜品id查询
        queryWrapper.eq(dishId != null,ShoppingCart::getDishId,dishId);

        //如果是套餐，就根据套餐id查询
        queryWrapper.eq(setmealId != null,ShoppingCart::getSetmealId,setmealId);

        ShoppingCart sc = shoppingCartService.getOne(queryWrapper);

        //如果已有这个套餐或菜品，
        if(sc != null ){
            //将购物车中的套餐或菜品已有的数量加1
            sc.setNumber(sc.getNumber() + 1);
            //更新
            shoppingCartService.updateById(sc);
            //将新的数量返回给shoppingCart用来返回给前端
            shoppingCart.setNumber(sc.getNumber());
        }else{//该用户没有这个套餐或菜品的订单
            //将number设为1
            shoppingCart.setNumber(1);
            //如果不存在，则还需设置一下创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            //存入数据库
            shoppingCartService.save(shoppingCart);
        }

        return R.success(shoppingCart);
    }

    //查询所有
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){

        //查询当前用户下的所有订单
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        Long userId = BaseContext.getCurrentId();

        queryWrapper.eq(ShoppingCart::getUserId,userId);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }


    //减少（购物车和菜单中）套餐或菜品的数量
    @PostMapping("/sub")
    public R<ShoppingCart> substract(@RequestBody ShoppingCart shoppingCart){

        //获取用户id
        Long userId = BaseContext.getCurrentId();

        Long dishId = shoppingCart.getDishId();

        Long setmealId = shoppingCart.getSetmealId();


        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        //只查当前用户的
        queryWrapper.eq(ShoppingCart::getUserId,userId);

        //如果是菜品，就根据菜品id查询
        queryWrapper.eq(dishId != null,ShoppingCart::getDishId,dishId);

        //如果是套餐，就根据套餐id查询
        queryWrapper.eq(setmealId != null,ShoppingCart::getSetmealId,setmealId);

        ShoppingCart sc = shoppingCartService.getOne(queryWrapper);


        Integer number = sc.getNumber()-1;

        //如果减少到0就直接删除
        if(number <= 0){
            shoppingCartService.removeById(sc.getId());
        }

        sc.setNumber(number);

        shoppingCartService.updateById(sc);
        return R.success(sc);

    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){

        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(queryWrapper);

        return R.success("清空购物车成功");
    }

}
